package com.lbssoft.gpsbd.controller;

import com.lbssoft.gpsbd.annotation.LogAnnotation;
import com.lbssoft.gpsbd.model.Result;
import com.lbssoft.gpsbd.service.CommandRecordService;
import com.lbssoft.gpsbd.service.CommandService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.hibernate.validator.constraints.NotEmpty;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Api("命令管理")
@RestController
@RequestMapping("/command")
public class CommandsController {

	@Autowired
	CommandService commandService;

	@Autowired
	CommandRecordService recordService;
	
	@ApiOperation(value="设备类型与对应的命令视图", notes="类型ID对应的命令下发数据格式")
	@RequestMapping(value = "/views",method = {RequestMethod.GET})
	@ResponseBody
	public Result views() {
		return commandService.getViews();
	}

	@ApiOperation(value="发送命令", notes="下发命令")
	@RequiresPermissions("user:common:commandsend")
	@LogAnnotation(targetType = "common",action = "add",remark = "下发命令")
	@RequestMapping(value = "/send",method = {RequestMethod.POST})
	@ResponseBody
	public Result send(@RequestBody Map command) {
			return commandService.send(command);
	}

	@ApiOperation(value="根据设备获取命令记录", notes="根据设备获取命令记录")
	@RequestMapping(value = "/records",method = {RequestMethod.GET})
	@ResponseBody
	public Result records(@NotEmpty @RequestParam(value = "deviceId", defaultValue = "") long deviceId,
					   @NotEmpty @RequestParam(value = "page", defaultValue = "1") int page,
					   @NotEmpty @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
		return recordService.list(deviceId,page,pageSize);
	}
	
}